package com.xx.pmis.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.xx.pmis.model.Function;
import com.xx.pmis.model.Role;
import com.xx.pmis.model.User;
import com.xx.pmis.services.RoleService;
import com.xx.pmis.services.UserService;

/**
 * @class 初始化菜单树 <br>
 * 
 * @editor 石星星 @Date 2011-12-30 上午10:33:07 <br>
 * @version jdk_1.5.0
 */
public class InitFunctionTree extends GenericController {
	private static final long serialVersionUID = 8343535911995333859L;
	private static final Logger logger = Logger.getLogger(InitFunctionTree.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 用户
		UserService userService = getPMISServices(request.getSession().getServletContext()).getUserService();
		// 角色
		RoleService roleService = getPMISServices(request.getSession().getServletContext()).getRoleService();
		String userName = (String) request.getSession().getAttribute("realName");
		User user = null;
		try {
			user = userService.getUserByUserName(userName);
		} catch (Exception e) {
			logger.info("根据用户名，查询该用户异常！" + e.toString());
		}
		PrintWriter writer = response.getWriter();
		Role role = null;
		Set<Function> select = new HashSet<Function>();
		try {
			role = roleService.getRoleByRoleID(user.getRole().getRoleID());
		} catch (Exception e) {
			logger.info("根据角色ID，查询该用户的角色异常！" + e.toString());
		}
		List<Function> functions = role.getFunction();
		
		Iterator<?> it = functions.iterator();
		while (it.hasNext()) {
			Function function = (Function) it.next();
			select.add(function);
		}
		writer.write(menu(select));
	}

	/**
	 * 排序
	 * 
	 * @param functions
	 * @return
	 */
	public List<Function> paixu(Set<Function> functions) {
		List<Function> a = new ArrayList<Function>();
		for (Function f : functions) {
			a.add(f);
		}
		FunctionComparator comp = new FunctionComparator();
		Collections.sort(a, comp);
		return a;
	}

	/**
	 * 树状菜单
	 */
	public String menu(Set<Function> s) {
		StringBuffer stringbuffer = new StringBuffer();
		stringbuffer.append("d = new dTree('d');\n");
		List<Function> all = this.paixu(s);
		for (Function f : all) {
			stringbuffer.append("d.add(").append(f.getFunctionID()).append(",").append(f.getFunctionParentID()).append(
					",'").append(f.getFunctioName()).append("','").append(f.getFunctionURL()).append("','").append(
					f.getFunctionTilte() != null ? f.getFunctionTilte() : "").append("','").append(
					f.getFunctionTarget() != null ? f.getFunctionTarget() : "").append("','").append(
					f.getFunctionIcon() != null ? f.getFunctionIcon() : "").append("','").append(
					f.getFunctionIconOpen() != null ? f.getFunctionIconOpen() : "").append("');\n");
		}
		stringbuffer.append("document.write(d);");
		return stringbuffer.toString();
	}

}

/**
 * 比较类
 * 
 * @author SHIXINGXING
 * 
 */
class FunctionComparator implements Comparator<Object> {
	public int compare(Object o1, Object o2) {
		Function c1 = (Function) o1;
		Function c2 = (Function) o2;
		if (c1.getFunctionID() > c2.getFunctionID()) {
			return 1;
		} else {
			if (c1.getFunctionID() == c2.getFunctionID()) {
				return 0;
			} else {
				return -1;
			}
		}
	}
}